import axios from 'axios';
import {Message} from 'element-ui';
import router from '../../router/index'
export function fetch (options) {
    // if(options.data && options.data.actions!==undefined){
    // options.url += '/actions/' + options.data.actions
    // }

    //除登录操作都需要带authKey、sessionId
    return new Promise((resolve, reject) => {
      let request = axios.create({
        baseURL: 'https://lottery.yhhvip.shop/yhh-lottery/', // 测试
        timeout: 10000, // 生产环境可以调大些
        headers: {
        'Content-Type': 'application/json',
        'token':localStorage.getItem('token')
        // 'authKey':Cookies.get('authKey'),
        // 'sessionId':Cookies.get('sessionId'),
        },
        // transformRequest: [function (data) {
        // // 在发送请求之前对请求数据做处理，form-data格式化
        // 　　data = Qs.stringify(data);
        // return data;
        // }],
      });
      // Message.info('请求一次');
      request(options)
      .then((response) => {
        // Message.success('返回一次');
        // console.log(response);
        if (response['status'] === 200 && response['data']['status'] == 200) {
          
          resolve(response['data'] || response["data"]["msg"] || response["data"]["status"] );
        } else {
          let error = response['data'];
          reject(response['data']);
          // Message.error(error['msg']);
          if( error.status == -1 && error.msg =="请先登录"){ 
            localStorage.clear();
            Message({
              message:'请先登录',
              type:'warning',
              duration:2000,
              onClose:function(){
                router.push('/login');
              }
          });
          }else{
            // Message.error(error['message']);
          }
        }
      }).catch((error) => {
        console.log(error);
        Message.error(res.msg);
        // Message.error('请求出错请联系管理员~');
      });
    });
  }